/**
 * @Author: leibidong
 * @Description:根据分类名查询商品
 * @File:  queryProductService
 * @Version: 1.0.0
 * @Date: 2021/7/5 11:52
 */

package service

import (
	"encoding/json"
	"fmt"
	"ginProject/biz/dao"
	"ginProject/biz/model"
	"ginProject/biz/utils"
	"ginProject/biz/vo"
	"github.com/go-redis/redis"
	"github.com/shopspring/decimal"
	"time"
)

/**
 * @Author leibidong
 * @Description 根据一级分类名查找所有的商品，如果没有输入一级分类名，则查找所有
 * @Date  2021/7/5 12:20
 **/
func SelectProduct(firstName string) []model.ProductInfo {
	rdb := dao.InitRedis()
	if len(firstName) != 0 {
		key := utils.GetProductInfo(firstName)
		val2, err := rdb.Get(key).Result()
		if err == redis.Nil || err != nil {
			id := model.QueryByName(firstName).ID
			p := model.SelectAllProductById(id)
			if p == nil {
				return nil
			}
			ub := utils.XuLieHua(p)
			rdb.Set(key, ub, 86400*time.Second)
			return p
		} else {
			var val []model.ProductInfo
			_ = json.Unmarshal([]byte((val2)), &val)
			return val
		}
	} else {
		p := model.SelectAllProduct()
		return p
	}

}

/**
 * @Author leibidong
 * @Description 管理员添加商品
 * @Date  2021/7/5 12:55
 **/
func AddProduct(product vo.AddProductMessage) {
	t := utils.GetTime()
	productType := utils.GetIndex(product.ColorType)
	aver, _ := decimal.NewFromString(product.AverageCost)
	price, _ := decimal.NewFromString(product.Price)
	fmt.Println(productType, "  ", aver, " 转换结果", price)
	fmt.Println(product.ColorType)
	productInfo := model.ProductInfo{Encoding: product.Encoding, Name: product.Name, Price: price,
		AverageCost: aver, RecommendDescript: product.RecommendDescript, Descript: product.Descript,
		OneCategoryId: utils.StringToInt(product.OneCategoryId), TwoCategoryId: utils.StringToInt(product.TwoCategoryId), CreateTime: t, ModifiedTime: t, ProductionDate: t,
		ColorType: productType}
	fmt.Println("结果", productInfo)
	model.InsertProduct(productInfo)
	productId := model.SelectIdByProductName(product.Name, product.Encoding).ID
	productpicInfo := model.ProductPicInfo{ProductId: productId, PicUrl: product.ProductPicUrl,
		PicDesc: product.ProductPicDesc, IsMaster: 1, CreateTime: t, ModifiedTime: t}
	model.InsertProductPic(productpicInfo)
}

/**
 * @Author leibidong
 * @Description 更新商品信息
 * @Date  2021/7/5 15:25
 **/
func UpdateProductMessage(up vo.UpdateProductMessage) int {
	p := model.SelectId(up.Id)
	if p == (model.ProductInfo{}) {
		return 0
	} else {
		val, _ := decimal.NewFromString(up.Price)
		val1, _ := decimal.NewFromString(up.AverageCost)
		typeIndex := utils.GetIndex(up.ColorType)
		s := string(up.Encoding)
		up1 := model.ProductInfo{Descript: up.Descript, RecommendDescript: up.RecommendDescript, Encoding: s,
			ID: up.Id, Price: val, AverageCost: val1, ColorType: typeIndex, Name: up.Name}
		model.UpdateProductMessage(up1)
		categoryName := model.SelectFirstCategoryNameByProductId(up.Id).Name
		key := utils.GetProductSecondCategory(categoryName)
		rdb := dao.InitRedis()
		rdb.Del(key)
		return 1
	}
}

/**
 * @Author leibidong
 * @Description 根据商品id删除商品
 * @Date  2021/7/6 18:49
 **/
func DeleteProduct(id int) int {
	p := model.SelectId(id)
	if p == (model.ProductInfo{}) {
		return 0
	} else {
		model.DeleteProduct(id)
		categoryName := model.SelectFirstCategoryNameByProductId(id).Name
		key := utils.GetProductSecondCategory(categoryName)
		rdb := dao.InitRedis()
		rdb.Del(key)
		return 1
	}
}
